//
//  UserInfos.swift
//  GitOSC-Swift
//
//  Created by 李萍 on 16/1/26.
//  Copyright © 2016年 李萍. All rights reserved.
//

import UIKit
import SwiftKeychainWrapper

class UserInfos: NSObject {
    let userIdKey = "id"
    let userNameKey = "username"
    let nameKey = "name"
    let bioKey = "bio"
    let weiboKey = "weibo"
    
    let blogKey = "blog"
    let themeIdKey = "theme_id"
    let stateKey = "state"
    let createAtKey = "create_at"
    let portraitKey = "portrait"
    
    let emailKey = "email"
    let private_tokenKey = "private_token"
    let adminKey = "admin"
    let canCreateGroupKey = "canCreateGroup"
    let canCreateProjectKey = "canCreateProject"
    
    let canCreateTeamKey = "canCreateTeam"
    let followerCountKey = "followerCount"
    let staredCountKey = "staredCount"
    let followingCountKey = "followingCount"
    let watchedCountKey = "watchedCount"
    
    func saveUserInfos(user : GTUser) -> Void {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        
        userDefaults.setInteger(user.userID, forKey: userIdKey)
        userDefaults.setObject(user.userName, forKey: userNameKey)
        userDefaults.setObject(user.name, forKey: nameKey)
        userDefaults.setObject(user.bio, forKey: bioKey)
        userDefaults.setObject(user.weibo, forKey: weiboKey)
        
        userDefaults.setObject(user.blog, forKey: blogKey)
        userDefaults.setInteger(user.themeId, forKey: themeIdKey)
        userDefaults.setObject(user.state, forKey: stateKey)
        userDefaults.setObject(user.createdAt, forKey: createAtKey)
        userDefaults.setObject(user.portrait, forKey: portraitKey)
        
        userDefaults.setObject(user.email, forKey: emailKey)
        userDefaults.setObject(user.private_token, forKey: private_tokenKey)
        userDefaults.setBool(user.admin, forKey: adminKey)
        userDefaults.setBool(user.canCreateGroup, forKey: canCreateGroupKey)
        userDefaults.setBool(user.canCreateProject, forKey: canCreateProjectKey)
        
        userDefaults.setBool(user.canCreateTeam, forKey: canCreateTeamKey)
        userDefaults.setInteger(user.followerCount, forKey: followerCountKey)
        userDefaults.setInteger(user.staredCount, forKey: staredCountKey)
        userDefaults.setInteger(user.followingCount, forKey: followingCountKey)
        userDefaults.setInteger(user.watchedCount, forKey: watchedCountKey)
        
        userDefaults.synchronize()
    }
    
    func removeUserInfos() -> Void {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        
        userDefaults.removeObjectForKey(userIdKey)
        userDefaults.removeObjectForKey(userNameKey)
        userDefaults.removeObjectForKey(nameKey)
        userDefaults.removeObjectForKey(bioKey)
        userDefaults.removeObjectForKey(weiboKey)
        
        userDefaults.removeObjectForKey(blogKey)
        userDefaults.removeObjectForKey(themeIdKey)
        userDefaults.removeObjectForKey(stateKey)
        userDefaults.removeObjectForKey(createAtKey)
        userDefaults.removeObjectForKey(portraitKey)
        
        userDefaults.removeObjectForKey(emailKey)
        userDefaults.removeObjectForKey(private_tokenKey)
        userDefaults.removeObjectForKey(adminKey)
        userDefaults.removeObjectForKey(canCreateGroupKey)
        userDefaults.removeObjectForKey(canCreateProjectKey)
        
        userDefaults.removeObjectForKey(canCreateTeamKey)
        userDefaults.removeObjectForKey(followerCountKey)
        userDefaults.removeObjectForKey(staredCountKey)
        userDefaults.removeObjectForKey(followingCountKey)
        userDefaults.removeObjectForKey(watchedCountKey)
    }
    
    func getUserID() -> NSInteger {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        var userId = userDefaults.valueForKey(userIdKey) as? NSInteger
        
        if userId == nil {
            userId = 0
        }
        return userId!
    }
    
    func getprivateToken() -> String {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        
        var privateToken = userDefaults.valueForKey(private_tokenKey) as? String
        
        if privateToken == nil {
            privateToken = ""
        }
        
        return privateToken!
    }
    
    func saveLoginPassword(account: String, password: String) -> Void {
        let saveSuccessful:Bool = KeychainWrapper.setString(password, forKey:account)
        
        print("saveSuccessful=\(saveSuccessful)")
    }
}

/*
let StrUsernameKey:String = "username"
let StrPasswordKey:String = "password"
let saveSuccessful:Bool=KeychainWrapper.setString(usernameTextField.text!, forKey:StrUsernameKey)
print("saveSuccessful=\(saveSuccessful)") //saveSuccessful=true
let retrievedString: String? = KeychainWrapper.stringForKey(StrUsernameKey)
print("retrievedString=\(retrievedString)") //retrievedString=Optional("xxx")
let removeSuccessful: Bool = KeychainWrapper.removeObjectForKey(StrUsernameKey)
print("removeSuccessful=\(removeSuccessful)") //removeSuccessful=true
let retrievedStringAfterDelete: String? = KeychainWrapper.stringForKey(StrUsernameKey)
print("retrievedStringAfterDelete=\(retrievedStringAfterDelete)") //retrievedStringAfterDelete=nil
*/

